﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.EnterpriseCommands
{
    public class EnterpriseSelectHandler : IRequestHandler<EnterpriseSelectCommand, ResultPage<EnterDto>>
    {
        public readonly IBaseRepository<Enterprise> enterpriseRepository;
        public readonly IBaseRepository<Province> provinceRepository;
        public readonly IBaseRepository<City> cityRepository;
        public readonly IBaseRepository<County> countyRepository;
        public readonly IBaseRepository<IndustryLabels> industryLabelsRepository;
        public readonly IBaseRepository<Industry> industryTrpository;
        public readonly IBaseRepository<AccountingUnits> accountingUnitsRepository;

        public EnterpriseSelectHandler(IBaseRepository<Enterprise> enterpriseRepository, IBaseRepository<Province> provinceRepository, IBaseRepository<City> cityRepository, IBaseRepository<County> countyRepository, IBaseRepository<IndustryLabels> industryLabelsRepository, IBaseRepository<Industry> industryTrpository, IBaseRepository<AccountingUnits> accountingUnitsRepository)
        {
            this.enterpriseRepository = enterpriseRepository;
            this.provinceRepository = provinceRepository;
            this.cityRepository = cityRepository;
            this.countyRepository = countyRepository;
            this.industryLabelsRepository = industryLabelsRepository;
            this.industryTrpository = industryTrpository;
            this.accountingUnitsRepository = accountingUnitsRepository;
        }



        public async Task<ResultPage<EnterDto>> Handle(EnterpriseSelectCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<EnterDto>() { Code=(int)CodeEnum.search,totalCount=0,totalPage=0};
            try
            {
                var list = (from a in enterpriseRepository.GetAll()
                            join b in provinceRepository.GetAll()
                            on a.ProvinceId equals b.ProvinceId
                            join c in cityRepository.GetAll()
                            on a.CityId equals c.CityId
                            join d in countyRepository.GetAll()
                            on a.CountyId equals d.CountyId
                            join e in industryLabelsRepository.GetAll()
                            on a.IndustryLabelsId equals e.IndustryLabelsId
                            join f in accountingUnitsRepository.GetAll()
                            on a.AccountingUnitId equals f.AccountingUnitId
                            join g in industryTrpository.GetAll()
                            on a.IndustryId equals g.IndustryId
                            select new EnterDto()
                            {
                                EnterpriseID = a.EnterpriseID,
                                EnterpriseFullName = a.EnterpriseFullName,
                                EnterpriseAbbreviation = a.EnterpriseAbbreviation,
                                EnterpriseUpload = a.EnterpriseUpload,
                                IndustryId = g.IndustryId,
                                IndustryLabelsName= e.IndustryLabelsName,
                                IndustryLabelsId = a.IndustryLabelsId,
                                IndustryName= g.IndustryName,
                                ProvinceId = a.ProvinceId,
                                ProvinceName = b.ProvinceName,
                                CityId = a.CityId,
                                CityName = c.CityName,
                                CountyId = a.CountyId,
                                CountyName = d.CountyName,
                                EnterpriseAddress = a.EnterpriseAddress,
                                EnterprisePhone = a.EnterprisePhone,
                                EnterpriseUnified = a.EnterpriseUnified,
                                EnterpriseDescription = a.EnterpriseDescription,
                                EnterpriseDeactivated = false,
                                EnterpriseHead = a.EnterpriseHead,
                                EnterpriseFinance = a.EnterpriseFinance,
                                EnterpriseCharge = a.EnterpriseCharge,
                                EnterprisePerson = a.EnterprisePerson,
                                AccountingUnitId = a.AccountingUnitId,
                                AccountingUnitName = f.AccountingUnitName,
                            }).ToList();
                result.totalCount = list.Count();
                result.totalPage = (int)Math.Ceiling(result.totalCount * 1.0 / request.pagesize);
                list = list.OrderBy(x => x.EnterpriseID).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                result.Data = list;
                return result;
                //data.PageCount = list.Count;
                //data.TotalCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pagesize);
                //data.data = list.OrderBy(x => x.EnterpriseID).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                //res.Code = (int)CodeEnum.success;
                //res.Data = data;
                //res.Messege = "列表展示";
            }
            catch (Exception)
            {
                throw;
            }
            //return res;
        }


        
    }
}
